﻿/*************************************************
 * 
 *  框架级
 *  
 *  数据库通用类 - 执行SQL添加操作
 * 
 *  create by ternence
 * 
 *  create datetime at 2017-03-17
 *  
 *  西安佳卓信息科技有限公司 ©2017
 * 
 * ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Dapper;
using System.Data;

namespace Zor_IDAL
{
    public static class DapperInsert<T> where T : class
    {

        /// <summary>
        /// 执行增加
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="t">对象</param>
        /// <returns>框架应答</returns>
        public static BaseAnswer DoInsert(string sql, T t)
        {
            try
            {
                using (IDbConnection conn = DapperConn.OpenSqlConnection())
                {
                    var Answer = conn.Execute(sql, t);
                    return Answer > 0 ? new BaseAnswer {  AnswerType = BaseAnswer.Answers.Succee  } : new BaseAnswer { AnswerType  = BaseAnswer.Answers.Failure };
                }
            }
            catch (Exception ex)
            {
                return new BaseAnswer { AnswerType = BaseAnswer.Answers.Error, AnswerMeg = ex.Message };
            }

        }

        /// <summary>
        /// 执行增加 
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="obj">参数，支持批量</param>
        /// <returns>框架应答</returns>
        public static BaseAnswer DoInsert(string sql, object obj)
        {
            try
            {
                using (IDbConnection conn = DapperConn.OpenSqlConnection())
                {
                    var Answer = conn.Execute(sql, obj);
                    return Answer > 0 ? new BaseAnswer { AnswerType = BaseAnswer.Answers.Succee } : new BaseAnswer { AnswerType = BaseAnswer.Answers.Failure };
                }
            }
            catch (Exception ex)
            {
                return new BaseAnswer { AnswerType = BaseAnswer.Answers.Error, AnswerMeg = ex.Message };
            }

        }

    }
}
